package com.zyg.seckill.controller;

import com.zyg.common.utils.MyDate;
import com.zyg.seckill.entity.TbSeckillGoods;
import com.zyg.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    private SeckillService seckillService;
    //秒杀列表页面
    @GetMapping({"/","seckill-index"})
    public String index(Model model){
        //第一部分：将当前时间菜单放到model中
        //得到当前时间开始的五个时间段
        List<Date> dateMenus = MyDate.getDateMenus();
        //将得到的五个时间段放到model中
        model.addAttribute("menus",dateMenus);

        //第二部分：将当前的秒杀商品列表放到model中
        //根据当前时间段得到秒杀商品列表
        List<TbSeckillGoods> goodsList = seckillService.getSeckillGoods();
        //放到model中
        model.addAttribute("goodsList",goodsList);
        return "seckill-index";
    }

    //根据秒杀商品id查询此秒杀商品
    @GetMapping("/seckill/findSeckillGoods/{id}")
    public String findSeckillGoods(@PathVariable Integer id,Model model){
        //根据商品id从redis中查询
        TbSeckillGoods seckillGoods = seckillService.findSeckillGoods(id);
        //放到model中
        model.addAttribute("seckillGoods",seckillGoods);
        //返回视图名
        return "seckill-item";
    }
}
